package com.jiangjiang.eshop.promotion.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.jiangjiang.eshop.promotion.biz.PromotionActivityBiz;
import com.jiangjiang.eshop.promotion.dto.PromotionActivityDTO;
import com.jiangjiang.eshop.promotion.dto.PromotionActivityQueryPageDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName PromotionActivityController
 * @Description 营销活动管理
 * @Author jhw
 * @Date 2021/7/29 11:23
 * @Version 1.0
 **/
@Slf4j
@RestController
@RequestMapping("/promotion/activity")
public class PromotionActivityController {

    @Autowired
    private PromotionActivityBiz promotionActivityBiz;

    /**
     * 新增促销活动
     *
     * @param dto 促销活动
     */
    @PostMapping("/save")
    public void save(@RequestBody PromotionActivityDTO dto) {
        try {
            promotionActivityBiz.save(dto);
        } catch (Exception e) {
            log.warn("error", e);
        }
    }

    /**
     * 营销活动更新
     *
     * @param dto 促销活动
     */
    @PostMapping("/updatePromotion")
    public void updatePromotion(@RequestBody PromotionActivityDTO dto) {
        try {
            promotionActivityBiz.updatePromotion(dto);
        } catch (Exception e) {
            log.warn("error", e);
        }

    }


    /**
     * 分页查询营销活动数据
     *
     * @param dto 营销活动分页查询条件DTO
     * @return 分页数据
     */
    @PostMapping("/queryForPage")
    public Page<PromotionActivityDTO> queryForPage(@RequestBody PromotionActivityQueryPageDTO dto) {
        try {
            return promotionActivityBiz.queryForPage(dto);
        } catch (Exception e){
            log.warn("error",e);
        }
        return new Page<>();
    }


    /**
     * 根据商品id 查询促销活动
     *
     * @param goodsId 商品id
     * @return 促销活动
     */
    @GetMapping("/listByGoodsId")
    public List<PromotionActivityDTO> listByGoodsId( @RequestParam("goodsId") Long goodsId) {
        try {
            return promotionActivityBiz.listByGoodsId(goodsId);
        }catch (Exception e){
            log.warn("error",e);
        }
        return Lists.newArrayList();
    }

    /**
     * 根据id查询优惠活动
     *
     * @param id 营销活动id
     * @return 营销活动DTO
     */
    @GetMapping("/queryById")
    public PromotionActivityDTO queryById(@RequestParam("id") Long id) {
        try {
            return promotionActivityBiz.queryById(id);
        }catch (Exception e){
            log.warn("error",e);
        }
        return null;
    }

    /**
     * 删除促销活动
     *
     * @param promotionActivityId 促销活动id
     */
    @GetMapping("/removeByActivityId")
    public void removeByActivityId(@RequestParam("promotionActivityId") Long promotionActivityId) throws Exception {
        try {
            promotionActivityBiz.removeByActivityId(promotionActivityId);
        }catch (Exception e){
            log.warn("error",e);
        }

    }
}
